package com.quwan.common.service.impl;

import com.quwan.common.enums.LabelEnum;
import com.quwan.common.enums.SuggestionEnum;
import com.quwan.common.exception.QuWanException;
import com.quwan.common.result.ApiResult;
import com.quwan.common.service.ContentSecurityService;
import com.quwan.config.yml.TencentCloudApiConfig;
import com.quwan.utils.Base64Convert;
import com.quwan.utils.ContentSecurityUtil;
import com.quwan.utils.FIleCheckUtils;
import com.tencentcloudapi.ims.v20201229.models.ImageModerationResponse;
import com.tencentcloudapi.tms.v20201229.models.TextModerationResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Iterator;

@Service
@Slf4j
public class ContentSecurityServiceImpl implements ContentSecurityService, BeanPostProcessor {
    @Autowired
    private TencentCloudApiConfig tencentCloudApiConfig;

    private ContentSecurityUtil contentSecurityUtil;
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        String secretId = tencentCloudApiConfig.getSecretId();
        String secretKey = tencentCloudApiConfig.getSecretKey();
        String tmsEndpoint = tencentCloudApiConfig.getTmsEndpoint();
        String imsEndpoint = tencentCloudApiConfig.getImsEndpoint();
        String region = tencentCloudApiConfig.getRegion();
        contentSecurityUtil = new ContentSecurityUtil(secretId,secretKey,tmsEndpoint,imsEndpoint,region);
        return bean;
    }


    @Override
    public ApiResult checkContent(String content) {
//        contentSecurityUtil.createTmsClient();
//        TextModerationResponse response = contentSecurityUtil.getTextModerationResponse(content);
//        if (response != null){
//            log.info(response.getLabel());
//            if (response.getLabel().equals(LabelEnum.Normal.getMsg())){
//                if (response.getSuggestion().equals(SuggestionEnum.Pass.getMsg())){
//                    return ApiResult.successMsg(String.format("文本内容：%s %s",content,"检测通过"));
//                }
//                return ApiResult.failed(String.format("文本内容：%s %s",content,"检测未通过"));
//            }
//            return ApiResult.failed(String.format("文本内容：%s %s",content,"检测未通过"));
//        }
//        return ApiResult.failed(String.format("文本内容：%s %s",content,"检测未通过"));
        return ApiResult.successMsg(String.format("文本内容：%s %s",content,"检测通过"));
    }

    @Override
    public ApiResult checkImage(HttpServletRequest request) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Iterator it = multipartRequest.getFileNames();
            if (!it.hasNext()) {
                throw new QuWanException("文件名称不能空");
            }
            try {
                contentSecurityUtil.createImsClient();
                while (it.hasNext()) {
                    String fieldName = (String) it.next();
                    MultipartFile file = multipartRequest.getFile(fieldName);
                    // 文件名
                    String originalFileName = file.getOriginalFilename();
                    // 上传文件校验
                    File changeFile = FIleCheckUtils.checkUserImg(file);
                    return ApiResult.successMsg(String.format("图片：%s %s",originalFileName,"检测通过"));
//                    ImageModerationResponse response = contentSecurityUtil.getImageModerationResponse(Base64Convert.fileToBase64(file));
//                    if (response != null){
//                        log.info(response.getLabel());
//                        if (response.getLabel().equals(LabelEnum.Normal.getMsg())){
//                            if (response.getSuggestion().equals(SuggestionEnum.Pass.getMsg())){
//                                return ApiResult.successMsg(String.format("图片：%s %s",originalFileName,"检测通过"));
//                            }
//                            return ApiResult.failed(String.format("图片：%s %s",originalFileName,"检测未通过"));
//                        }
//                        return ApiResult.failed(String.format("图片：%s %s",originalFileName,"检测未通过"));
//                    }
//                    return ApiResult.failed(String.format("图片：%s %s",originalFileName,"检测未通过"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ApiResult.failed();
    }
}
